﻿#region Copyright (c) 2012-11, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

using System;
using System.Globalization;
using Amarok.Agents.Resources;


namespace Amarok.Agents
{
	/// <summary>
	/// This type represents a Message. A Message is an object that can be exchanged between Agents to transfer 
	/// information, to trigger an operation, to query for data, to coordinate Agents or for any other purpose
	/// you might come up with.
	/// 
	/// In general, Messages are implemented as reference types that must derive directly or indirectly from this 
	/// base class. It is good design practice to implement Messages as immutable types so that accidental 
	/// modifications are impossible. This is important since Messages will be transfered to other Agents, thus,
	/// it is very likely that the content of those Messages will be accessed in other threads concurrently.
	/// 
	/// Messages should contain copies of data (entities) or some kind of unique identifier (identities). Latter 
	/// can then be used to query for that identified entities. Never transfer object references to private Agent 
	/// data, except for global shared data that never changes; always transfer copies of data.
	/// </summary>
	[Serializable]
	public abstract class Message :
		IEquatable<Message>
	{
		// data
		private Guid mId;
		private Object mLabel;


		#region ++ Public Interface ++

		/// <summary>
		/// Gets the globally unique id of this message object.
		/// </summary>
		public Guid Id
		{
			get
			{
				return mId;
			}
		}


		/// <summary>
		/// Gets or sets the label of this Message. This property can be used to label Messages with additional 
		/// information. Later, this information can be used in label-based filters to decide whether Messages 
		/// should be forwarded to certain Agents.
		/// 
		/// The label can be set only once by assigning a non-null object to this property. Trying to overwrite
		/// the label, after it has been set, will result in an exception. You have to reset it first by calling 
		/// <see cref="ResetLabel"/> before you can set the label once again.
		/// </summary>
		/// 
		/// <value>
		/// A null reference when no label is assigned to this Message; otherwise the object representing the label.
		/// </value>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		/// <exception cref="InvalidOperationException">
		/// The label of a message can be set only once. You have to reset it first before you can set the label 
		/// once again.</exception>
		public Object Label
		{
			get
			{
				return mLabel;
			}
			set
			{
				Verify.NotNull(value, "Label");

				if (this.HasLabel)
					throw new InvalidOperationException(AgentResources.MessageLabelCannotBeSetTwice);

				mLabel = value;
			}
		}

		/// <summary>
		/// Gets a boolean value indicating whether this Message has a label assigned to it.
		/// </summary>
		public Boolean HasLabel
		{
			get
			{
				return mLabel != null;
			}
		}


		/// <summary>
		/// Resets the label of this Message to a null reference. After calling this method, it is possible 
		/// to set the label once again.
		/// </summary>
		public void ResetLabel()
		{
			mLabel = null;
		}

		#endregion

		#region ++ Public Interface (GetHashCode, Equals, ToString) ++

		/// <summary>
		/// Returns the hash code for the current Message instance. 
		/// </summary>
		/// 
		/// <returns>
		/// A 32-bit signed integer hash code.</returns>
		public override Int32 GetHashCode()
		{
			return mId.GetHashCode();
		}

		/// <summary>
		/// Determines whether the specified Message instance is equal to the current instance.
		/// </summary>
		/// 
		/// <param name="other">
		/// The Message instance to compare with the current instance.</param>
		/// 
		/// <returns>
		/// True, if the specified Message instance is equal to the current instance; otherwise, False.</returns>
		public override Boolean Equals(Object other)
		{
			return Equals(other as Message);
		}

		/// <summary>
		/// Determines whether the specified Message instance is equal to the current instance.
		/// </summary>
		/// 
		/// <param name="other">
		/// The Message instance to compare with the current instance.</param>
		/// 
		/// <returns>
		/// True, if the specified Message instance is equal to the current instance; otherwise, False.</returns>
		public Boolean Equals(Message other)
		{
			// null reference?
			if ((Object)other == null)
				return false;

			// same object?
			if ((Object)other == (Object)this)
				return true;

			// compare via fields
			return other.mId == this.mId;
		}

		/// <summary>
		/// Returns a string that represents the current Message instance.
		/// </summary>
		/// 
		/// <returns>
		/// A string that represents the current Message instance.</returns>
		public override String ToString()
		{
			return String.Format(CultureInfo.InvariantCulture, "{0}(Id: {1}, Label: {2})",
				this.GetType().FullName,
				mId.ToString("B").ToUpperInvariant(),
				mLabel ?? "<null>");
		}

		#endregion

		#region ## Protected Interface ##

		/// <summary>
		/// Initializes a new instance.
		/// </summary>
		protected Message()
		{
			mId = Guid.NewGuid();
		}

		#endregion

	}
}
